﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using SAPbobsCOM;

namespace BoAddonBiz_FreightForwarder.Sea
{
    [BoAddonBiz_FreightForwarder.BusinessBase.ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_SE_Delivery)]
    public class B1Form_Delivery_Sea : B1Form_BaseOperationSon
    {
        public override string ThisSubTableName
        {
            get { return null; }
        }

        public override string ThisSubTableName2
        {
            get { return null; }
        }

        public override string FatherField
        {
            get { return "U_IsDelivery"; }
        }

        public override string FatherUDS
        {
            get { return "ODLV_UID"; }
        }

        public override string ThisTableName
        {
            get { return "@AVA_SE_ODLV"; }
        }

        public override string ThisObjectCode
        {
            get { return "AVA_SE_Deliver"; }
        }

        public B1Form_Delivery_Sea()
            : base()
        {
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Delivery_frm_ChooseFormListEvent);
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Delivery_frm_ItemEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_Delivery_Sea_frm_FormDataEvent);
        }

        void B1Form_Delivery_Sea_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {

            }
            else
            {
                if ((pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE))//主对象保存成功之后
                {
                    this.UpdateSEDLData();
                }
            }
        }

        void B1Form_Delivery_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
            }
            else
            {
                if (pVal.ItemUID == "1000006" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                {
                    SelectCargo();
                }
            }
        }

        void B1Form_Delivery_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                SAPbouiCOM.Conditions cons = default(SAPbouiCOM.Conditions);
                SAPbouiCOM.Condition con = default(SAPbouiCOM.Condition);
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_OCRD1":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup12";//12收货人
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD2":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup11";//11发货人
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCRD3":/*发货单位*/
                    case "CFL_OCRD4":/*接货单位*/
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup5";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup11";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup12";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCPR_Issue":/*发货联系人*/
                        Utils.ChooseFromListAddress_Filter(this.CurrentForm, ref cfl, ThisTableName, "U_PickupCode", cons, con, ref  BubbleEvent);
                        break;
                    case "CFL_OCPR_Rec":/*接货联系人*/
                        Utils.ChooseFromListAddress_Filter(this.CurrentForm, ref cfl, ThisTableName, "U_ConsigneeCode", cons, con, ref  BubbleEvent);
                        break;
                }
            }
            else
            {
                if (pVal.Action_Success)
                {
                    dynamic Customerinfo;
                    SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName);
                    if (pVal.ChooseFromListUID == "CFL_OCRD1")//客户
                    {
                        dbds.SetValue("U_CardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_CardName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD4")//接货公司
                    {
                        dbds.SetValue("U_ConsigneeCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsigneeName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());

                        Customerinfo = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsigneeAddress", dbds.Offset, Customerinfo.Address);
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD3")//提货地址
                    {
                        dbds.SetValue("U_PickupCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_PickupName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        Customerinfo = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_PickupAddress", dbds.Offset, Customerinfo.Address);
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD2")//运输供应商
                    {
                        dbds.SetValue("U_ConsignorCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsignorName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        Customerinfo = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsignorCntct", dbds.Offset, Customerinfo.FullName);

                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCPR_Issue")/*发货联系人*/
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName);
                        dbds.SetValue("U_PickupAddress", dbds.Offset, Utils.GetAddress(pVal));
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCPR_Rec")/*接货联系人*/
                    {
                        dbds.SetValue("U_ConsigneeAddress", dbds.Offset, Utils.GetAddress(pVal));
                    }
                }
            }
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_SE_Delivery;
            FormSetting.FormFileName = "AVA_SE_DeliverForm.srf";
            base.FormInfoSetting(FormSetting);
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("95"), "Code", "Name", "@AVA_FF_OPRI", "Code", null, "U_Type = N'Number' OR U_Type = N'Pack'", true, false, true);// 包装形式 取值 计量单位里的 by WangPeng 2012年8月29日 18:56:43

            base.InitializeForm(InitPar);
            if (!String.IsNullOrEmpty(InitPar.FatherFormUID) && CurrentFormEx.Mode == BoFormMode.fm_ADD_MODE)
            {
                this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_Weight", 0, "0");
                //总重量，总体积，包装形式等，需要从表头代入一次。并添加件数，尺寸和总计费重字段，也从表头代入数据。
                Form listForm = SBOApp.Forms.Item(InitPar.FatherFormUID.Split(';')[0]);
                Form BNOForm = SBOApp.Forms.Item(listForm.DataSources.UserDataSources.Item("ParentUID").Value);

                ComboBox cb = this.CurrentForm.Items.Item("1000006").Specific;
                for (int i = 0; i < BNOForm.DataSources.DBDataSources.Item("@AVA_SE_BNO1").Size; i++)
                {
                    cb.ValidValues.Add(BNOForm.DataSources.DBDataSources.Item("@AVA_SE_BNO1").GetValue("LineId", i).Trim(), BNOForm.DataSources.DBDataSources.Item("@AVA_SE_BNO1").GetValue("U_CargoName", i).Trim());
                }
                if (cb.ValidValues.Count > 0)
                {
                    cb.Select(0, BoSearchKey.psk_Index);
                    SelectCargo();
                }
            }
        }

        private void SelectCargo()
        {
            this.CurrentForm.Freeze(true);
            ComboBox cb = this.CurrentForm.Items.Item("1000006").Specific;
            string lineId = cb.Value.Trim();
            this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_BaseLine", 0, lineId);
            Form listForm = SBOApp.Forms.Item(this.CurrentForm.DataSources.UserDataSources.Item("ParentUID").Value);
            Form BNOForm = SBOApp.Forms.Item(listForm.DataSources.UserDataSources.Item("ParentUID").Value);
            DBDataSource dbds_bno1 = BNOForm.DataSources.DBDataSources.Item("@AVA_SE_BNO1");
            for (int i = 0; i < dbds_bno1.Size; i++)
            {
                if (dbds_bno1.GetValue("LineId", i).Trim() == lineId)
                {
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoSize", 0, dbds_bno1.GetValue("U_CargoSize", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoQuantity", 0, dbds_bno1.GetValue("U_Quantity", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_TotalVolumn", 0, dbds_bno1.GetValue("U_Volume", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_EstimateGross", 0, dbds_bno1.GetValue("U_GrossWeight", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_Weight", 0, dbds_bno1.GetValue("U_BillingWeight", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_PackagedForm", 0, dbds_bno1.GetValue("U_PackForm", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoType", 0, dbds_bno1.GetValue("U_CargoType", i));
                    break;
                }
            }
            this.CurrentForm.Freeze(false);
        }

        #region 更新SEDL字段
        private void UpdateSEDLData()
        {
            String BaseDocEntry = CurrentForm.DataSources.DBDataSources.Item(ThisTableName).GetValue("U_BaseEntry", 0).Trim();
            if (string.IsNullOrEmpty(BaseDocEntry))
            {
                return;
            }
            String SQL_UpdSEDL = @"
UPDATE T1 SET 
T1.U_DeliveryDate=T3.U_ActualArriveDate --实际送货日期
FROM [@AVA_SE_SEDL] T1
INNER JOIN [@AVA_SE_OBNO] T2 ON T1.U_SBLNumber=ISNULL(T2.U_SBLNumber,N'')
INNER JOIN [@AVA_SE_ODLV] T3 ON T2.DocEntry=T3.U_BaseEntry
WHERE T2.DocEntry={0} ";
            Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            try
            {
                rs.DoQuery(string.Format(SQL_UpdSEDL, BaseDocEntry));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
